package convert

import (
	"gitee.com/kristas/booting-go/framework/common/util/lang"
	"strconv"
)

var pattern = map[string]func(string) interface{}{
	"^true$": func(s string) interface{} {
		return true
	},
	"^false$": func(s string) interface{} {
		return false
	},
	"^[0-9]*$": func(s string) interface{} {
		i, _ := strconv.ParseInt(s, 10, 64)
		return i
	},
}

func Inference(src string) (interface{}, error) {
	s := lang.NewString(src)
	for p, f := range pattern {
		matches, err := s.Matches(p)
		if err != nil {
			return nil, err
		}
		if matches {
			return f(s.String()), nil
		}
	}
	return src, nil
}
